<?php

namespace App\Http\Controllers\Admin\Anchor;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Anchor\AnchorAuthorRepository;
use App\Rules\AnchorAuthorValidate;
use Illuminate\Http\Request;

// 达人
class AuthorController extends BaseController
{
    private $repoist;

    public function __construct(AnchorAuthorRepository $repository)
    {
        $this->repoist = $repository;
    }


    public function index(Request $request)
    {
        $where = [];
        if ($request->has('nickname') && !empty($request->get('nickname'))) {
            $where['where']['nickname'] = $request->get('nickname');
        }
        if ($request->has('author_id') && !empty($request->get('author_id'))) {
            $where['where']['author_id'] = $request->get('author_id');
        }
        if (($request->has('status')) && ($request->get('status') != '') && ($request->get('status') != -1)) {
            $where['where']['status'] = (int)$request->get('status');
        }
//        print_r($request);
        if ($request->has('dateRange') && !empty($request->get('dateRange'))) {
            $time = $request->get('dateRange');
            $where['between'] = ['created_at' => [$time]];
        }
        $dataArr = $this->repoist->index($where, $request->get('pageNum'), $request->get('pageSize'));

        if (is_array($dataArr)) {
            $dataArr['statusList'] = config('card.status');
        }
        return $dataArr;
    }

    public function edit(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        $Validator = new AnchorAuthorValidate();
        if (!$Validator->check($request->all())) {
            return $this->returnMsg(1, $Validator->getError());
        }

//      return $this->repoist->unifiedEdit($request, [], 'author_id');
        return $this->repoist->unifiedEdit($request, ['id' => '请指定修改的数据'], []);
    }

    public function add(Request $request)
    {
        $Validator = new AnchorAuthorValidate();
        if (!$Validator->check($request->all())) {
            return $this->returnMsg(1, $Validator->getError());
        }
        return $this->repoist->unifiedAdd($request, [], 'author_id');
    }

    public function del(Request $request, $id)
    {
        if (!$id) {
            return $this->returnMsg(1, '请传递参数');
        }
        return $this->repoist->unifiedDel($id);
    }

    public function changeStatus(Request $request)
    {
        $id = $request->get("id");
        // 需要修改两个地方 一个是状态 一个 app是否上架
        $status = (int)$request->get('status');
        return $this->repoist->unifiedUpdateStatus($id, $status);
    }

    //    返回名字给前端做选择
    public function optionList(Request $request)
    {
//        if (!$request->has('anchor_id')) {
//            return $this->returnMsg(1, '请传递主播参数', []);
//        }
//        $anchor_id = $request->anchor_id;
        $data = $this->repoist->unifiedGetAll(['status' => 0], ['id', 'author_id', 'nickname']);
        return $this->returnMsg(200, '数据已获取', $data);
    }
}
